package com.nsy.shortlink.admin.controller;

import com.nsy.shortlink.admin.common.convention.exception.ClientException;
import com.nsy.shortlink.admin.common.convention.result.Result;
import com.nsy.shortlink.admin.common.convention.result.Results;
import com.nsy.shortlink.admin.common.enums.UserErrorCodeEnum;
import com.nsy.shortlink.admin.dto.req.UserLoginReqDTO;
import com.nsy.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.nsy.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.nsy.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.nsy.shortlink.admin.dto.resp.UserRespDTO;
import com.nsy.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @className: UserController
 * @author: 宁舒意
 * @description: TODO
 * @date: 2024/9/25 17:45
 */
@RestController
@RequiredArgsConstructor
public class UserController {


    private final UserService userService;

    /**
     * 根据用户名查询用户信息
    **/
    @GetMapping("/api/short-link/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable String username){
        UserRespDTO user = userService.getUserByUsername(username);
        if(user==null){
            throw  new ClientException(UserErrorCodeEnum.USER_NULL);
        }else {
            return Results.success(user);
        }
    }

    /**
     * 判断用户名是否存在
     * @author 宁舒意
     * @date 17:13 2024/9/27
     * @param username
     * @return com.nsy.shortlink.admin.common.convention.result.Result<java.lang.Boolean>
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 用户注册
     * @author 宁舒意
     * @date 17:16 2024/9/27
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO param){
        userService.register(param);
        return  Results.success();
    }

    /**
     * 修改用户
    **/
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }


    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success();
    }


}
